package com.zjy.pet.service.impl;

import com.zjy.basic.commonenum.ErrorCode;
import com.zjy.basic.exception.CustomException;
import com.zjy.basic.util.AjaxResult;
import com.zjy.basic.util.BaiduAuditUtils;
import com.zjy.basic.util.CurrentLoginInfoUtil;
import com.zjy.org.domain.Employee;
import com.zjy.org.mapper.EmployeeMapper;
import com.zjy.pet.domain.Pet;
import com.zjy.pet.domain.PetDetail;
import com.zjy.pet.domain.PetOnlineAuditLog;
import com.zjy.pet.mapper.PetDetailMapper;
import com.zjy.pet.mapper.PetMapper;
import com.zjy.pet.mapper.PetOnlineAuditLogMapper;
import com.zjy.pet.service.PetService;
import com.zjy.basic.service.impl.BaseServiceImpl;
import com.zjy.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements PetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Override
    @Transactional
    public void add(Pet pet) {
        //暂时只能写死
        pet.setShopId(39L);
        super.add(pet);
        PetDetail petDetail = pet.getDetail();
        if (petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.add(petDetail);
        }
    }

    @Override
    @Transactional
    public AjaxResult onsale(Long[] ids, HttpServletRequest request) throws Exception {
        //获取当前登录用户的信息
        Logininfo logininfo = CurrentLoginInfoUtil.currentLoginInfo(request);
        //通过登录信息获取员工，做审核人
        Employee employee = employeeMapper.findByLoginInfoId(logininfo.getId());
        //判断登录信息是否存在
        if (logininfo == null){
            throw new CustomException("上架失败");
        }
        //遍历 选中要上架的pet的id
        for (Long id:ids){
            //1.先取得对应id的对象
            Pet pet = petMapper.findById(id);
            //2.审核：百度智能审核
            Boolean b = BaiduAuditUtils.TextCensor(pet.getName());
            if (b){//如果审核成功（都为true）
                //添加审核日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(1);
                log.setAuditId(employee.getId());
                log.setNote("审核成功");
                petOnlineAuditLogMapper.add(log);

                petMapper.updateOnSaleState(id,new Date());
            }else {//审核不通过
                //添加审核日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(0);
                log.setAuditId(employee.getId());
                log.setNote("审核失败");
                petOnlineAuditLogMapper.add(log);

                return AjaxResult.me().setSuccess(false).setMsg("上架失败");
            }
        }
        return new AjaxResult();
    }

    @Override
    public AjaxResult offsale(Long[] ids) {
        petMapper.updateState(ids,new Date());
        return AjaxResult.me();
    }
}
